<?php

namespace App\Http\Service;

use Illuminate\Support\Facades\Log;

class BaseService {

    /**
     * @var string 信息
     */
    protected String $message;

    /**
     * @var string 错误代码
     * 0 无异常
     */
    protected string $errorCode;

    /**
     * 设置错误代码和信息
     * @param int $code 错误代码
     * @param string $message 错误信息
     * @return bool
     */
    public function setMessage(int $code, string $message=''): bool {
        $this->errorCode = $code;
        $this->message = $message;
        Log::error(json([
            "code" => $code,
            "message" => $message
        ]));
        return true;
    }

    /**
     * 获取错误信息
     * @return array
     */
    public function getMessage(): array {
        $data = [
            "code" => $this->errorCode,
            "message" => $this->message
        ];
        return $data;
    }
}
